package utils;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

public class Interval {
	private double x;
	private double y;
	
	private Boolean isInterval = false;
	
	/**
	 * Represents interval [0, 0]
	 */
	public Interval() {
		x = 0.0;
		y = 0.0;
	}
	
	/**
	 * In this version class will be only a container for Double (convertible to Double)
	 * @param x
	 */
	public Interval(double x) {
		this.x = x;
		this.y = Double.NaN;
		isInterval = false;
	}
	
	/**
	 * Represents interval [x, y]
	 * @param x
	 * @param y
	 */
	public Interval(double x, double y) {
		this.x = x;
		this.y = y;
		
		isInterval = true;
	}
	
	public String toString() {
		DecimalFormatSymbols symbol = new DecimalFormatSymbols();
		symbol.setDecimalSeparator('.');
		DecimalFormat twoDForm = new DecimalFormat("0.000", symbol);
		
		if (isInterval) {
			return "[" + twoDForm.format(x) +", " + twoDForm.format(y) +"]";
		} else {
			return "[" + twoDForm.format(x) + "]";
		}
	}
	
	public Double toDouble() {
		if (isInterval) {
			throw new UnsupportedOperationException("It is not possible to convert interval to Double");
		}
		return x;
	}
	
	// Setters and getters
	public void setX(double x) {
		this.x = x;
	}
	
	public void setY(double y) {
		this.y = y;
		isInterval = true;
	}
	
	public void setSame(double x) {
		this.x = x;
		this.y = Double.NaN;
		isInterval = false;
	}
	
	public void setInterval(double x, double y) {
		this.x = x;
		this.y = y;
		isInterval = true;
	}
	
	public double getX() {
		return x;
	}
	
	public double getY() {
		return y;
	}
	
	public boolean isInterval() {
		return this.isInterval;
	}
}
